﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using Model;

namespace DAL
{
    public class D_AdminUser : IAdminUser
    {
        WishDayEntities _e;
        public D_AdminUser()
        {
            _e = new WishDayEntities();
        }
        public void Add(AdminUser obj)
        {
            _e.AdminUser.Add(obj);
            _e.SaveChanges();
        }

        public void Delete(int id)
        {
            _e.AdminUser.Remove(_e.AdminUser.Find(id));
            _e.SaveChanges();
        }

        public AdminUser FindById(int id)
        {
            return _e.AdminUser.Find(id);
        }

        public List<AdminUser> GetAll()
        {
            return _e.AdminUser.ToList();
        }

        public AdminUser Login(string username, string password)
        {
            var data = _e.AdminUser.FirstOrDefault(x => x.AdminName == username && x.PassWord == password);
            if (data==null)
            {
                return null;
            }
            return data;
        }

        public void Update(int id, AdminUser obj)
        {
            var data = _e.AdminUser.Find(id);
            data.AdminName = obj.AdminName;
            data.PassWord = obj.PassWord;
            data.NickName = obj.NickName;
            data.HeadImg = obj.HeadImg;
            data.IsEnable = obj.IsEnable;
            data.Power = obj.Power;
            data.CreateTime = obj.CreateTime;
            _e.SaveChanges();
        }

        public int UpPass(int id, string passwod)
        {
            var data = _e.AdminUser.Find(id);
            if (data==null)
            {
                return 0;
            }
            else
            {
                data.PassWord = passwod;
                _e.SaveChanges();
                return 1;
            }
        }
    }
}
